package cn.yld.car.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.yld.car.common.Loger;
import com.yld.car.domain.DialogCityItem;

/**
 * 查询省份数据库的工具类
 * 
 * 
 */
public class DialogCityDB {

	private static DialogCityDBManager dbm;
	private static SQLiteDatabase db;
	private static Loger log = new Loger(DialogCityDB.class.getSimpleName());

	private DialogCityDB() {
	}

	/**
	 * 查询所有的省份信息
	 * 
	 * @param context
	 *            Context
	 * @return List<MyListItem>
	 */
	public static List<DialogCityItem> getAllProInfo(Context context) {
		dbm = new DialogCityDBManager(context);
		dbm.openDatabase();
		db = dbm.getDatabase();
		List<DialogCityItem> list = new ArrayList<DialogCityItem>();
		try {
			String sql = "SELECT DISTINCT (province) FROM CityRegion";
			Cursor cursor = db.rawQuery(sql, null);
			log.output("cursor:" + cursor.getCount());
			int i = 0;
			while (cursor.moveToNext()) {
				i++;
				String province = cursor.getString(cursor
						.getColumnIndex("province"));
				DialogCityItem myListItem = new DialogCityItem();
				myListItem.setName(province);
				list.add(myListItem);
			}
			log.output("i:" + i);
		} catch (Exception e) {
			log.output("提取所有省份信息异常", e);
		}
		dbm.closeDatabase();
		return list;
	}

	/**
	 * 返回所有省份的字符串信息
	 * 
	 * @param context
	 * @return
	 */
	public static List<String> getAllProInfoStr(Context context) {
		dbm = new DialogCityDBManager(context);
		dbm.openDatabase();
		db = dbm.getDatabase();
		List<String> list = new ArrayList<String>();
		try {
			String sql = "SELECT DISTINCT (province) FROM CityRegion";
			Cursor cursor = db.rawQuery(sql, null);
			log.output("cursor:" + cursor.getCount());
			int i = 0;
			while (cursor.moveToNext()) {
				i++;
				String province = cursor.getString(cursor
						.getColumnIndex("province"));
				list.add(province);
			}
			log.output("i:" + i);
		} catch (Exception e) {
			log.output("提取所有省份信息异常", e);
		}
		dbm.closeDatabase();
		return list;
	}

	/**
	 * 获取指定省份的城市信息
	 * 
	 * @param context
	 *            Context
	 * @param pro
	 *            String
	 * @return List<MyListItem>
	 */
	public static List<DialogCityItem> getCityInfo(Context context, String pro) {
		dbm = new DialogCityDBManager(context);
		dbm.openDatabase();
		db = dbm.getDatabase();
		List<DialogCityItem> list = new ArrayList<DialogCityItem>();
		try {
			String sql = "select DISTINCT(city) from CityRegion where province = ?";
			Cursor cursor = db.rawQuery(sql, new String[] { pro });
			while (cursor.moveToNext()) {
				String city = cursor.getString(cursor.getColumnIndex("city"));
				DialogCityItem myListItem = new DialogCityItem();
				myListItem.setName(city);
				list.add(myListItem);
			}
		} catch (Exception e) {
			log.output("提取指定省份信息异常，省份：" + pro, e);
		}
		dbm.closeDatabase();
		db.close();
		return list;
	}

	public static List<String> getCityInfoStr(Context context, String pro) {
		dbm = new DialogCityDBManager(context);
		dbm.openDatabase();
		db = dbm.getDatabase();
		List<String> list = new ArrayList<String>();
		try {
			String sql = "select DISTINCT(city) from CityRegion where province = ?";
			Cursor cursor = db.rawQuery(sql, new String[] { pro });
			while (cursor.moveToNext()) {
				String city = cursor.getString(cursor.getColumnIndex("city"));
				list.add(city);
			}
		} catch (Exception e) {
			log.output("提取指定省份信息异常，省份：" + pro, e);
		}
		dbm.closeDatabase();
		db.close();
		return list;
	}

	/**
	 * 获取指定城市下的县级信息
	 * 
	 * @param context
	 *            Context
	 * @param pro
	 *            String 省份
	 * @param city
	 *            String 城市
	 * @return List<MyListItem>
	 */
	public static List<DialogCityItem> getCountyInfo(Context context,
			String pro, String city) {
		dbm = new DialogCityDBManager(context);
		dbm.openDatabase();
		db = dbm.getDatabase();
		List<DialogCityItem> list = new ArrayList<DialogCityItem>();
		try {
			String sql = "select code,area,desc from CityRegion where province = ? and city = ? ";
			Cursor cursor = db.rawQuery(sql, new String[] { pro, city });
			while (cursor.moveToNext()) {
				String area = cursor.getString(cursor.getColumnIndex("area"));
				String code = cursor.getString(cursor.getColumnIndex("code"));
				String desc = cursor.getString(cursor.getColumnIndex("desc"));
				DialogCityItem myListItem = new DialogCityItem();
				myListItem.setName(area);
				myListItem.setPcode(code);
				myListItem.setDesc(desc);
				list.add(myListItem);
			}
			cursor.close();
		} catch (Exception e) {
			log.output("提取指定的县级信息异常，省份：" + pro + "  城市：" + city, e);
		}
		dbm.closeDatabase();
		db.close();
		return list;
	}
}